import { CaseSchema } from '@src/components/schema/caseSchema'
import moment, { Moment } from 'moment'

// 获得表单初始化数据
export function getInitialValuesByCaseDetail (d: CaseSchema | null): { [k: string]: any } {
  return {
    ...initBlockOne(d),
    ...initBlockTwo(d),
    ...initBlockThree(d),
    ...initBlockFour(d),
    ...initBlockFive(d),
    ...initBlockSix(d)
  }
}

// 初始化案件流程日期的数据
function initBlockOne (d: CaseSchema | null): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {
      hearing_datetime_list: [undefined],
      arbitrator_assign_datetime_list: [undefined]
    }
  }
  if (typeof d.accept_datetime === 'number') {
    r.accept_datetime = moment(d.accept_datetime * 1000)
  }
  if (typeof d.docket_datetime === 'number') {
    r.docket_datetime = moment(d.docket_datetime * 1000)
  }
  if (typeof d.fee_pay_datetime === 'number') {
    r.fee_pay_datetime = moment(d.fee_pay_datetime * 1000)
  }
  if (typeof d.preservation_datetime === 'number') {
    r.preservation_datetime = moment(d.preservation_datetime * 1000)
  }
  if (typeof d.to_process_datetime === 'number') {
    r.to_process_datetime = moment(d.to_process_datetime * 1000)
  }
  if (typeof d.arrive_datetime === 'number') {
    r.arrive_datetime = moment(d.arrive_datetime * 1000)
  }
  if (typeof d.arbitrator_select_datetime === 'number') {
    r.arbitrator_select_datetime = moment(d.arbitrator_select_datetime * 1000)
  }
  if (typeof d.arbitrator_report_datetime === 'number') {
    r.arbitrator_report_datetime = moment(d.arbitrator_report_datetime * 1000)
  }
  if (d.arbitrator_assign_datetime_list instanceof Array && d.arbitrator_assign_datetime_list.length > 0) {
    const list = d.arbitrator_assign_datetime_list.filter(li => typeof li === 'number' && li > 0)
    if (list.length > 0) {
      r.arbitrator_assign_datetime_list = list.map((li: number) => {
        return moment(li * 1000)
      })
    } else {
      r.arbitrator_assign_datetime_list = [undefined]
    }
  } else {
    r.arbitrator_assign_datetime_list = [undefined]
  }
  if (d.hearing_datetime_list instanceof Array && d.hearing_datetime_list.length > 0) {
    const list = d.hearing_datetime_list.filter(li => typeof li === 'number' && li > 0)
    if (list.length > 0) {
      r.hearing_datetime_list = list.map((li: number) => {
        return moment(li * 1000)
      })
    } else {
      r.hearing_datetime_list = [undefined]
    }
  } else {
    r.hearing_datetime_list = [undefined]
  }
  if (typeof d.closure_report_datetime === 'number') {
    r.closure_report_datetime = moment(d.closure_report_datetime * 1000)
  }
  if (typeof d.closure_audit_datetime === 'number') {
    r.closure_audit_datetime = moment(d.closure_audit_datetime * 1000)
  }
  if (typeof d.closure_datetime === 'number') {
    r.closure_datetime = moment(d.closure_datetime * 1000)
  }
  if (typeof d.file_datetime === 'number') {
    r.file_datetime = moment(d.file_datetime * 1000)
  }
  return r
}

// 初始化程序复杂度
function initBlockTwo (d: CaseSchema | null): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {}
  }
  if (d.trial_limit_deduct_list instanceof Array && d.trial_limit_deduct_list.length > 0) {
    r.trial_limit_deduct_list = d.trial_limit_deduct_list.map(li => {
      const o: { [k: string]: any } = { type: li.type }
      if (typeof li.begin_datetime === 'number') {
        o.begin_datetime = moment(li.begin_datetime * 1000)
      }
      if (typeof li.end_datetime === 'number') {
        o.end_datetime = moment(li.end_datetime * 1000)
      }
      o.days = li.days
      return o
    })
  } else {
    r.trial_limit_deduct_list = [{ }]
  }
  const keys = ['closure_docpage', 'pre_closure_count', 'investigate_count', 'appraisal_count', 'challenge_count', 'jurisdiction_object_count', 'claimamend_count', 'counterclaim_count', 'reassign_arbitrator_count', 'party_add_count', 'expert_discuss_count', 'expert_report_count']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    }
  })
  return r
}

// 初始化程序复杂度
function initBlockThree (d: CaseSchema | null): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {}
  }
  const keys = ['break_rule_count', 'break_rule_desc', 'fix_arbprocedure_score', 'fix_arbprocedure_desc', 'fix_arbtity_score', 'fix_arbtity_desc']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    }
  })
  return r
}
// 初始化受理工作量
function initBlockFour (d: CaseSchema | null): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {}
  }
  const keys = ['preservation_count', 'fee_derate_count', 'supgreement_count']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    }
  })
  return r
}

// 初始化办案抽查
function initBlockFive (d: CaseSchema | null): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {}
  }
  const keys = ['closure_docpage', 'pre_closure_count', 'typo_count', 'miscalculate_count', 'mismaterial_count', 'mispostmaterial_score', 'misfile_count', 'misprocedure_count', 'misarbtity_count', 'misabc_desc', 'misabc_score']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    }
  })
  if (d.processer_notice_list instanceof Array && d.processer_notice_list.length > 0) {
    r.processer_notice_list = d.processer_notice_list
  } else if (d.score_status === '抽查中') {
    r.processer_notice_list = [{}]
  } else {
    r.processer_notice_list = []
  }
  if (typeof d.misprocedure_desc === 'string') {
    r.misprocedure_desc = d.misprocedure_desc.split(',')
  } else {
    r.misprocedure_desc = []
  }
  if (typeof d.misarbtity_desc === 'string') {
    r.misarbtity_desc = d.misarbtity_desc.split(',')
  } else {
    r.misarbtity_desc = []
  }
  return r
}

// 初始化受理抽查
function initBlockSix (d: CaseSchema | null): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {}
  }
  const keys = ['accept_misaccept_count', 'accept_miscaseinfo_count', 'accept_misfile_count', 'accept_mismaterial_count', 'accept_mispostmaterial_count', 'accept_miscalculate_score', 'accept_misprocedure_count', 'accept_typo_count', 'accept_miscalculate_count']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    }
  })
  if (d.accepter_notice_list instanceof Array && d.accepter_notice_list.length > 0) {
    r.accepter_notice_list = d.accepter_notice_list
  } else if (d.score_status === '抽查中') {
    r.accepter_notice_list = [{}]
  } else {
    r.accepter_notice_list = []
  }
  return r
}

// 格式化提交数据
export function formatSubmitData (d: { [k: string]: any }, scoreStatus: string): { [k: string]: any } {
  if (['抽查中'].includes(scoreStatus)) {
    return {
      ...formatBlockFive(d),
      ...formatBlockSix(d)
    }
  }
  return {
    ...formatBlockOne(d),
    ...formatBlockTwo(d),
    ...formatBlockThree(d),
    ...formatBlockFour(d)
  }
}

// 格式化案件流程日期的数据以提交
function formatBlockOne (d: { [k: string]: any }): { [k: string]: any } {
  const r: { [k: string]: any } = {
    accept_datetime: null,
    docket_datetime: null,
    fee_pay_datetime: null,
    preservation_datetime: null,
    to_process_datetime: null,
    arrive_datetime: null,
    arbitrator_select_datetime: null,
    arbitrator_report_datetime: null,
    arbitrator_assign_datetime_list: [],
    hearing_datetime_list: [],
    closure_report_datetime: null,
    closure_audit_datetime: null,
    closure_datetime: null,
    file_datetime: null
  }
  if (d.accept_datetime instanceof moment) {
    r.accept_datetime = Math.floor((d.accept_datetime as Moment).valueOf() / 1000)
  }
  if (d.docket_datetime instanceof moment) {
    r.docket_datetime = Math.floor((d.docket_datetime as Moment).valueOf() / 1000)
  }
  if (d.fee_pay_datetime instanceof moment) {
    r.fee_pay_datetime = Math.floor((d.fee_pay_datetime as Moment).valueOf() / 1000)
  }
  if (d.preservation_datetime instanceof moment) {
    r.preservation_datetime = Math.floor((d.preservation_datetime as Moment).valueOf() / 1000)
  }
  if (d.to_process_datetime instanceof moment) {
    r.to_process_datetime = Math.floor((d.to_process_datetime as Moment).valueOf() / 1000)
  }
  if (d.arrive_datetime instanceof moment) {
    r.arrive_datetime = Math.floor((d.arrive_datetime as Moment).valueOf() / 1000)
  }
  if (d.arbitrator_select_datetime instanceof moment) {
    r.arbitrator_select_datetime = Math.floor((d.arbitrator_select_datetime as Moment).valueOf() / 1000)
  }
  if (d.arbitrator_report_datetime instanceof moment) {
    r.arbitrator_report_datetime = Math.floor((d.arbitrator_report_datetime as Moment).valueOf() / 1000)
  }
  if (d.arbitrator_assign_datetime_list instanceof Array && d.arbitrator_assign_datetime_list.length > 0) {
    r.arbitrator_assign_datetime_list = d.arbitrator_assign_datetime_list.filter(li => li !== undefined && li !== null).map((li: Moment) => {
      return Math.floor(li.valueOf() / 1000)
    })
  }
  if (d.hearing_datetime_list instanceof Array && d.hearing_datetime_list.length > 0) {
    r.hearing_datetime_list = d.hearing_datetime_list.filter(li => li !== undefined && li !== null).map((li: Moment) => {
      return Math.floor(li.valueOf() / 1000)
    })
  }
  if (d.closure_report_datetime instanceof moment) {
    r.closure_report_datetime = Math.floor((d.closure_report_datetime as Moment).valueOf() / 1000)
  }
  if (d.closure_audit_datetime instanceof moment) {
    r.closure_audit_datetime = Math.floor((d.closure_audit_datetime as Moment).valueOf() / 1000)
  }
  if (d.closure_datetime instanceof moment) {
    r.closure_datetime = Math.floor((d.closure_datetime as Moment).valueOf() / 1000)
  }
  if (d.file_datetime instanceof moment) {
    r.file_datetime = Math.floor((d.file_datetime as Moment).valueOf() / 1000)
  }
  return r
}

// 格式化程序复杂度
function formatBlockTwo (d: { [k: string]: any }): { [k: string]: any } {
  const r: { [k: string]: any } = { trial_limit_deduct_list: [] }
  if (d === null) {
    return {}
  }
  const keys = ['closure_docpage', 'pre_closure_count', 'investigate_count', 'appraisal_count', 'challenge_count', 'jurisdiction_object_count', 'claimamend_count', 'counterclaim_count', 'reassign_arbitrator_count', 'party_add_count', 'expert_discuss_count', 'expert_report_count']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    }
  })
  if (d.trial_limit_deduct_list instanceof Array && d.trial_limit_deduct_list.length > 0) {
    r.trial_limit_deduct_list = d.trial_limit_deduct_list.filter(li => {
      return typeof li.type === 'string' && li.begin_datetime instanceof moment && li.end_datetime instanceof moment
    }).map((li: { type: string, begin_datetime: Moment, end_datetime: Moment }) => {
      const o: { type: string, begin_datetime?: number, end_datetime?: number } = { type: li.type }
      if (li.begin_datetime instanceof moment) {
        o.begin_datetime = Math.floor(moment(li.begin_datetime.format('YYYY-MM-DD')).valueOf() / 1000)
      }
      if (li.end_datetime instanceof moment) {
        o.end_datetime = Math.floor(moment(li.end_datetime.format('YYYY-MM-DD')).valueOf() / 1000)
      }
      return o
    })
  }
  return r
}

// 格式化程序复杂度
function formatBlockThree (d: { [k: string]: any }): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {}
  }
  const keys = ['break_rule_count', 'break_rule_desc', 'fix_arbprocedure_score', 'fix_arbprocedure_desc', 'fix_arbtity_score', 'fix_arbtity_desc']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    } else {
      r[key] = null
    }
  })
  return r
}

// 初始化受理工作量
function formatBlockFour (d: { [k: string]: any }): { [k: string]: any } {
  const r: { [k: string]: any } = {}
  if (d === null) {
    return {}
  }
  const keys = ['preservation_count', 'fee_derate_count', 'supgreement_count']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    } else {
      r[key] = null
    }
  })
  return r
}

// 初始化办案抽查
function formatBlockFive (d: { [k: string]: any }): { [k: string]: any } {
  const r: { [k: string]: any } = {
    misprocedure_desc: null,
    misarbtity_desc: null,
    processer_notice_list: []
  }
  if (d === null) {
    return {}
  }
  const keys = ['closure_docpage', 'pre_closure_count', 'typo_count', 'miscalculate_count', 'mismaterial_count', 'mispostmaterial_score', 'misfile_count', 'misprocedure_count', 'misarbtity_count', 'misabc_desc', 'misabc_score']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    } else {
      r[key] = null
    }
  })
  if (d.misprocedure_desc instanceof Array && d.misprocedure_desc.length > 0) {
    r.misprocedure_desc = d.misprocedure_desc.join()
  }
  if (d.misarbtity_desc instanceof Array && d.misarbtity_desc.length > 0) {
    r.misarbtity_desc = d.misarbtity_desc.join()
  }
  if (d.processer_notice_list instanceof Array && d.processer_notice_list.length > 0) {
    r.processer_notice_list = d.processer_notice_list.filter(v => {
      return v.key !== null && v.key !== undefined && v.count !== null && v.count !== undefined
    }).map(li => {
      return {
        key: li.key,
        count: li.count
      }
    })
  }
  return r
}

// 初始化受理抽查
function formatBlockSix (d: { [k: string]: any }): { [k: string]: any } {
  const r: { [k: string]: any } = { accepter_notice_list: [] }
  if (d === null) {
    return {}
  }
  const keys = ['accept_misaccept_count', 'accept_miscaseinfo_count', 'accept_misfile_count', 'accept_mismaterial_count', 'accept_mispostmaterial_count', 'accept_miscalculate_score', 'accept_misprocedure_count', 'accept_typo_count', 'accept_miscalculate_count']
  keys.forEach(key => {
    if (d[key] !== undefined) {
      r[key] = d[key]
    } else {
      r[key] = null
    }
  })
  if (d.accepter_notice_list instanceof Array && d.accepter_notice_list.length > 0) {
    r.accepter_notice_list = d.accepter_notice_list.filter(v => {
      return v.key !== null && v.key !== undefined && v.count !== null && v.count !== undefined
    }).map(li => {
      return {
        key: li.key,
        count: li.count
      }
    })
  }
  return r
}
